package com.caogen.cg_wl_kt.base

import android.util.Log
import com.google.gson.Gson
import com.google.gson.JsonElement
import org.json.JSONArray
import org.json.JSONException

object Gl:BaseP() {

    fun <T> getStringToBean(jsonStr: JsonElement?, rows: Class<T>?): T? {
        try {
            val gson = Gson()
            return gson.fromJson(jsonStr, rows)
        } catch (e: Exception) {
            Log.e("HTTPERROR", "Gl->>>数据解析异常" + e.message)
        }
        return null
    }

    fun <T> getStringToBeanArray(jsonStr: JsonElement?, rows: Class<T>?): ArrayList<T>? {
        try {
            val gson = Gson()
            try {
                val jsonArray = JSONArray(jsonStr)
                val mlist = ArrayList<T>()
                for (i in 0 until jsonArray.length()) {
                    val jsonObject = jsonArray.getJSONObject(i).toString()
                    mlist.add(gson.fromJson(jsonObject, rows))
                }
                println(mlist)
                return mlist
            } catch (e: JSONException) {
                e.printStackTrace()
            }
        } catch (e: Exception) {
            Log.e("HTTPERROR", "Gl->>>数据解析异常" + e.message)
        }
        return null
    }
}